/**
 * Copyright 2010 Balaji Thennarangam.
 */
package com.kathez;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.kathez.business.IKathezDelegatableBusiness;
import com.kathez.dao.IKathezDao;
import com.kathez.logging.KathezLogger;

/**
 * The factory class to get the Business and DAO objects.
 * The mapping for the objects are stored in the XML file.
 * @author Balaji T
 *
 */
public class KathezObjectFactory extends Root {
	
	private static final Logger LOGGER = KathezLogger.getLogger(KathezObjectFactory.class); 
	private static Properties businessObjectsMap = new Properties();
	private static Properties daoObjectsMap = new Properties();
	
	static{
		try{
			businessObjectsMap.loadFromXML(new FileInputStream("C:\\balaji\\Kathez\\StdPort\\build\\classes\\businessobjects.xml"));
			daoObjectsMap.loadFromXML(new FileInputStream("C:\\balaji\\Kathez\\StdPort\\build\\classes\\daoobjects.xml"));
		}catch(FileNotFoundException fnf){
			LOGGER.log(Level.SEVERE, "File Not found during business objects loading", fnf);
		}catch(InvalidPropertiesFormatException ipf){
			LOGGER.log(Level.SEVERE, "Invalid properties format during business objects loading", ipf);
		}catch(IOException ioe){
			LOGGER.log(Level.SEVERE, "IO Exception during business objects loading", ioe);
		}
		
	}
	
	/**
	 * Factory method to get the Business Object
	 * @param callingClass
	 * @return
	 */
	public static IKathezDelegatableBusiness getBusinessObject(String callingClass){
		String businessClassName = businessObjectsMap.getProperty(callingClass);
		try {
			if(businessClassName != null)
				return (IKathezDelegatableBusiness) Class.forName(businessClassName).newInstance();
			else
				LOGGER.log(Level.SEVERE, "No Business object Mapping found for the calling class "+callingClass);
		} catch (InstantiationException e) {
			LOGGER.log(Level.SEVERE, "Instantiation Exception during business object creation from class "+businessClassName, e);
		} catch (IllegalAccessException e) {
			LOGGER.log(Level.SEVERE, "Illegal Access Exception during business object creation from class "+businessClassName, e);
		} catch (ClassNotFoundException e) {
			LOGGER.log(Level.SEVERE, "Class Not Found Exception during business object creation from class "+businessClassName, e);
		}
		return null;
	}
	
	/**
	 * Fact
	 * @param callingClass
	 * @return
	 */
	public static IKathezDao getDAOObject(String callingClass){
		String daoClassName = daoObjectsMap.getProperty(callingClass);
		try {
			return (IKathezDao) Class.forName(daoClassName).newInstance();
		} catch (InstantiationException e) {
			LOGGER.log(Level.SEVERE, "Instantiation Exception during business object creation from class "+daoClassName, e);
		} catch (IllegalAccessException e) {
			LOGGER.log(Level.SEVERE, "Illegal Access Exception during business object creation from class "+daoClassName, e);
		} catch (ClassNotFoundException e) {
			LOGGER.log(Level.SEVERE, "Class Not Found Exception during business object creation from class "+daoClassName, e);
		}
		return null;
	}
}
